--
--  TTSAppDelegate.applescript
--  Shellshock Check
--
--  Created by Thomas Jones on 27/09/2014.
--  Copyright (c) 2014 TomTec Solutions. All rights reserved.
--

script TTSAppDelegate
	property parent : class "NSObject"
	
	-- IBOutlets
	property window : missing value
	property label : missing value
    property articleButton : missing value
    property aboutWindow : missing value
	on applicationWillFinishLaunching:aNotification
		activate
		set outputText to ""
		try
			set outputText to do shell script "env x='() { :;}; echo vulnerable' bash -c 'echo hello'"
		on error errorString
			if errorString contains "bash: warning: x: ignoring function definition attempt bash: error importing function definition for" then
				tell label to setStringValue:"Your computer is safe from the Shellshock shell vulnerability. You've got nothing to worry about, all's fine."
			else
				tell label to setStringValue:"An unknown error occurred. Sorry :("
			end if
		end try
		if outputText contains "vulnerable" then
			tell label to setStringValue:"Your computer is not safe from the Shellshock shell vulnerability."
			tell articleButton to setHidden:0
		end if
	end applicationWillFinishLaunching:
	
	on openArticle:sender
		open location "http://www.macissues.com/2014/09/25/how-to-unofficially-fix-the-shell-shock-bash-vulnerability-in-os-x/"
	end openArticle:
    
    on summonAboutWindow_(sender)
        tell aboutWindow to makeKeyAndOrderFront:me
    end summonAboutWindow_
    
    on sendEmailToMe:sender
        open location "mailto:thomas@tomtecsolutions.com?subject=Shellshock%20Check"
    end sendEmailToMe
	
    on sendEmailToMe2_(sender)
        open location "mailto:thomas@tomtecsolutions.com?subject=Shellshock%20Check"
    end sendEmailToMe2_
    
	on applicationShouldTerminate:sender
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate:
    on applicationShouldTerminateAfterLastWindowClosed_(sender)
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
end script